package cn.jji8.QSGUI.Gui.Goods;

import cn.jji8.QSGUI.quick.logger.Logger;
import cn.jji8.QSGUI.quick.money.Money;
import cn.jji8.QSGUI.quick.tool.ConfgTool;
import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.maxgamer.quickshop.shop.Shop;

import java.util.ArrayList;
import java.util.List;

/**
 * 一个装饰控制器，用于显示装饰物品
 * */
public class Decorate implements Goods{
    Shop shop;
    String name;
    List<String> lore;
    Material material;
    int number;

    /**
     * 在类别构造出来后一定要调用一次的方法
     *
     * @param shop 对应的商店
     * @param yml  对应的配置文件
     * @param key  自己对应的key
     */
    @Override
    public void setGoods(Shop shop, YamlConfiguration yml, String key) {
        this.shop = shop;
        String wp = ConfgTool.getString(yml,key,"物品","bedrock");
        material = Material.getMaterial(wp.toUpperCase());
        if(material==null){
            Logger.putSevere(key+".物品 属性 "+wp+"错误");
        }
        name = ConfgTool.getString(yml,key,"名字",null);
        lore = ConfgTool.getStringList(yml,key,"传说",null,false);
        number = ConfgTool.getint(yml,key,"数量",1,false);
    }

    /**
     * 获取要在箱子中显示的物品
     *
     * @param player
     */
    @Override
    public ItemStack getItemStack(Player player) {
        if(material==null){
            return null;
        }
        ItemStack itemStack = new ItemStack(material);
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.setDisplayName(replaceAllVariable(name,player));
        itemMeta.setLore(replaceAllVariable(lore,player));
        itemStack.setItemMeta(itemMeta);
        itemStack.setAmount(number);
        return itemStack;
    }

    /**
     * 玩家点击此物品时调用的方法
     *
     * @param a
     */
    @Override
    public void click(InventoryClickEvent a) {

    }
    public List<String> replaceAllVariable(List<String> a,Player player){
        List<String> list = new ArrayList<>();
        for(String s:a){
            list.add(replaceAllVariable(s,player));
        }
        return list;
    }
    public String replaceAllVariable(String a,Player player){
        return a
                .replaceAll("%商品价格%",cn.jji8.QSGUI.quick.tool.Number.XianShiZiFu(shop.getPrice()))
                .replaceAll("%玩家余额%",cn.jji8.QSGUI.quick.tool.Number.XianShiZiFu(Money.getMoney(player)))
                .replaceAll("%玩家名字%",player.getName());
    }
}
